//
//  BkCommitOrderViewController.m
//  TuDianEducation
//
//  Created by lixinkeji on 2021/1/13.
//  Copyright © 2021 zhangbenchao. All rights reserved.
//

#import "BkCommitOrderViewController.h"
#import "LXAddressListViewController.h"
#import "BkConformBottomView.h"
#import "BkOrderAddressView.h"
#import "BkGoodsMessageView.h"
#import "BkYunfeeView.h"
#import "BkBeizhuView.h"
#import "JZPayViewController.h"
#import "BkDuihuanSucessViewController.h"
@interface BkCommitOrderViewController ()
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) BkConformBottomView *bottomView;
@property (nonatomic,strong) UIView *headerView;
@property (nonatomic,strong) BkOrderAddressView *addressView;
@property (nonatomic,strong) BkGoodsMessageView *messageView;
@property (nonatomic,strong) BkYunfeeView *yunfeeView;
@property (nonatomic,strong) BkBeizhuView *beizhuView;
@property (nonatomic,strong)  LXAddressModel*addressModel;
@end

@implementation BkCommitOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.tableView];
    self.title = @"提交订单";
    [self getmyAddressListRequst];
}
-(UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, TSScreenWidth, TSScaleW(200))];
        _headerView.backgroundColor = kSubBacColor;
        [_headerView addSubview:self.addressView];
        [_headerView addSubview:self.messageView];
        [_headerView addSubview:self.yunfeeView];
        [_headerView addSubview:self.beizhuView];
        _headerView.height = self.beizhuView.bottom;
    }
    return _headerView;
}
-(BkOrderAddressView *)addressView{
    if (!_addressView) {
        _addressView = [[BkOrderAddressView alloc]init];
        WS(weakSelf);
        _addressView.gotoAddressBlock = ^{
            LXAddressListViewController *vc = [[LXAddressListViewController alloc]init];
            vc.type = AddressListTypeEdting;
            vc.callBackBlock = ^(LXAddressModel * _Nonnull model) {
                weakSelf.addressModel = model;
            };
            [weakSelf naviPushVc:vc];
        };
    }
    return _addressView;
}
-(BkGoodsMessageView *)messageView
{
    if (!_messageView) {
        _messageView = [[BkGoodsMessageView alloc]init];
        _messageView.top = _addressView.bottom;
        _messageView.dataDic = self.dataDic;
        WS(weakSelf);
        _messageView.changeAmountBlock = ^(NSInteger amountCount) {
            NSString * newprice = weakSelf.dataDic[@"newprice"];
            if ([kIsmember intValue] == 0) {
                newprice = self.dataDic[@"oldprice"];
            }
            NSString * freight=weakSelf.dataDic[@"freight"];
            weakSelf.bottomView.priceLabel.text = [NSString stringWithFormat:@"共%ld件商品，合计:￥%.2f",amountCount,newprice.doubleValue*amountCount +freight.doubleValue *amountCount ];
            
            weakSelf.yunfeeView.yunfeeLabel.text = freight.doubleValue == 0?@"免运费":[NSString stringWithFormat:@"%.2f",freight.doubleValue *amountCount];
        };
    }
    return _messageView;
}
-(BkYunfeeView *)yunfeeView
{
    if (!_yunfeeView) {
        _yunfeeView = [[BkYunfeeView alloc]init];
        _yunfeeView.top = _messageView.bottom;
        NSString * freight=_dataDic[@"freight"];
        _yunfeeView.yunfeeLabel.text = freight.doubleValue == 0?@"免运费":[NSString stringWithFormat:@"%@",freight];
    }
    return _yunfeeView;
}
-(BkBeizhuView *)beizhuView{
    if (!_beizhuView) {
        _beizhuView = [[BkBeizhuView alloc]init];
        _beizhuView.top = _yunfeeView.bottom;
    }
    return _beizhuView;
}


-(BkConformBottomView *)bottomView{
    if (!_bottomView) {
        _bottomView = [[BkConformBottomView alloc]init];
        _bottomView.bottom = TSScreenHeight - Height_NavBar;
        WS(weakSelf);
        NSString * newprice = self.dataDic[@"newprice"];
        if ([kIsmember intValue] == 0) {
            newprice = self.dataDic[@"oldprice"];
        }
        NSString * freight=_dataDic[@"freight"];
        _bottomView.priceLabel.text = [NSString stringWithFormat:@"共1件商品，合计:￥%.2f",newprice.doubleValue +freight.doubleValue];
      
        _bottomView.conformBuyBlock = ^{
            [weakSelf getimmediatelypurchaseRequst];
        };
    }
    return _bottomView;
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, TSScreenWidth, TSScreenHeight - Height_NavBar - _bottomView.height) style:(UITableViewStyleGrouped)];
      
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableHeaderView = self.headerView;
       
        if (@available(iOS 11.0, *))
        {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _tableView.estimatedRowHeight = 0;
            _tableView.estimatedSectionHeaderHeight = 0;
            _tableView.estimatedSectionFooterHeight = 0;
        }
        else
        {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        
    }
    return _tableView;
}
-(BOOL)pamasIsOkOrNot{
   
       if (!self.dataDic) {
          // [SSTool error:SSKJLanguage(@"请输入您的手机号")];
           [MBProgressHUD showError:@"请重新加载数据"];
           return NO;
       }
    if (!self.addressModel.addressid.length) {
        [MBProgressHUD showError:@"请选择收货地址"];
        return NO;
    }
       
       
     
    return YES;
}
#pragma mark ----商品立即购买
//cmd    是    string    immediatelypurchase
//uid    是    string    用户id
//gid    是    string    商品id
//numbers    是    string    购买数量
//addressid    是    string    收货地址id
//remarks    否    string    备注
-(void)getimmediatelypurchaseRequst
{
    if (![self pamasIsOkOrNot]) {
        return;
    }
    NSDictionary * pamas = @{@"gid":_dataDic[@"gid"],@"numbers":[NSString stringWithFormat:@"%ld",_messageView.amountCount],@"addressid":self.addressModel.addressid,@"remarks":_beizhuView.textView.text?:@""};
       [NetWorkTools postConJSONWithUrl:@"immediatelypurchase" parameters:pamas success:^(id responseObject) {
           NSString *result = responseObject[@"result"];
           NSString *resultNote = responseObject[@"resultNote"];
           NSString *ordernum = responseObject[@"ordernum"];
          
           if (result.integerValue == 0) {
              
               JZPayViewController *vc = [[JZPayViewController alloc]init];
               vc.ordernum = ordernum;
               NSString * newprice = self.dataDic[@"newprice"];
               if ([kIsmember intValue] == 0) {
                   newprice = self.dataDic[@"oldprice"];
               }
               NSString * freight=self.dataDic[@"freight"];
               vc.moneyAmount = [NSString stringWithFormat:@"%.2f",self.messageView.amountCount *newprice.doubleValue + freight.doubleValue *self.messageView.amountCount];
               [self naviPushVc:vc];
               
           }else{
               [MBProgressHUD showError:resultNote];
           }
         //
       } fail:^(NSError *error) {
           
       }];
}
#pragma mark -----请求我的地址
-(void)getmyAddressListRequst
{

 NSDictionary * pamas = @{@"nowPage":[NSString stringWithFormat:@"%@",@"1"],@"pageCount":[NSString stringWithFormat:@"%d",kPage_Size]};
    [NetWorkTools postConJSONWithUrl:@"myAddressList" parameters:pamas success:^(id responseObject) {
        NSString *result = responseObject[@"result"];
        NSString *resultNote = responseObject[@"resultNote"];
        NSArray *array = responseObject[@"dataList"];
       
        if (result.integerValue == 0) {
            for (NSDictionary *dic  in array) {
                NSString *isdefault = dic[@"isdefault"];
                if (isdefault.integerValue == 1) {
                    LXAddressModel *model = [[LXAddressModel alloc]init];
                    [model setValuesForKeysWithDictionary:dic];
                    self.addressModel = model;
                    break;
                }
            }
            
        }else{
             [MBProgressHUD showError:resultNote];
        }
         
    } fail:^(NSError *error) {
        
    }];
}
-(void)setAddressModel:(LXAddressModel *)addressModel{
    _addressModel = addressModel;
    self.addressView.model = _addressModel;
    
}
@end
